package com.etc.controller;

import com.etc.entity.FxAdmin;
import com.etc.service.FxAdminService;
import com.etc.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@RequestMapping(value = "")
//跨域
@CrossOrigin
@Api(value = "管理员接口",description = "curd")
public class FxAdminController {
    @Autowired
    FxAdminService service;

    Logger logger = (Logger) LogManager.getLogger(getClass());

    //查询

    /**
     * 用于登入
     * 查询所有的管理员 name 和密码 password
     * @param name
     * @param password
     * @return FxAdmin
     */
    @ApiOperation(value = "", notes = "管理员登入",response = FxAdmin.class,code = 200)
//    @ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer")
    @GetMapping(value = "select_adminlogin")
    public ResponseData Select_AdminLogin(@RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "password", defaultValue = "") String password){
        List<FxAdmin> list = service.select_Login(name, password);
        ResponseData responseData = new ResponseData();
        responseData.setData(list);
        return responseData;
    }

    /**
     * 查询 id=？ 的管理员信息
     * @param id
     * @return FxAdmin
     */
    @ApiOperation(value = "", notes = "管理员根据id查询",response = FxAdmin.class,code = 200)
    @GetMapping(value = "select_admin/{id}")
    public FxAdmin SelectAmdin(@PathVariable int id){
        FxAdmin fxAdmin = service.selectByPrimaryKey(id);

        return fxAdmin;
    }

    /**
     * 分页查询操作
     * @param pageNo 1
     * @param pageSize 5
     * @param keywords ""
     * @return List<FxAdmin>
     */
    @ApiOperation(value = "", notes = "分页查询",response = FxAdmin.class,code = 200)
    @GetMapping(value = "select_adminByPage/")
    public List<FxAdmin> SelectAdminByPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize, @RequestParam(value = "keywords", defaultValue = "") String keywords){
        List<FxAdmin> list = service.selectAdminByPage(pageNo, pageSize, keywords);

        return list;
    }

    /**
     * 用于管理员注册
     * @param fxAdmin
     * @return ResponseData
     */
    @ApiOperation(value = "", notes = "管理员注册",response = FxAdmin.class,code = 200)
    @PutMapping(value = "insert_admin/")
    public ResponseData InsertAdmin(@RequestBody FxAdmin fxAdmin){
        boolean flag = service.insert(fxAdmin);
        ResponseData result = new ResponseData();

        if (!flag) {
            result.setMsg("failed");
        }
        logger.info(result);
        return result;
    }
}
